package gen;

import gen.model.IDatatype;
import gen.model.ModelAttribute;
import gen.model.ModelClass;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

import org.w3c.dom.Document;

public class GenerateModelClasses {
	static final String ROOT = "generated";

	public static void main(String[] args) throws Exception {
		Document doc = XMLUtil.parseXML(new File("uml14.xmi"));
		//
		Map<String, IDatatype> objects = new TreeMap<String, IDatatype>();
		LoadUtil.loadTypes(doc, "Data_Types", "datatype", objects);
		LoadUtil.loadTypes(doc, "Core", "core", objects);
		LoadUtil.loadTypes(doc, "Common_Behavior", "behavior", objects);
		LoadUtil.loadTypes(doc, "Use_Cases", "usecase", objects);
		LoadUtil.loadTypes(doc, "State_Machines", "state", objects);
		LoadUtil.loadTypes(doc, "Collaborations", "collab", objects);
		LoadUtil.loadTypes(doc, "Activity_Graphs", "activity", objects);
		LoadUtil.loadTypes(doc, "Model_Management", "model", objects);
		//
		generate(objects);
		//
		System.out.println("Done");
	}

	private static void generate(Map<String, IDatatype> objects) throws IOException {
		for (Entry<String, IDatatype> entry : objects.entrySet()) {
			if (!entry.getValue().getName().equals("Boolean") && //
					!entry.getValue().getName().equals("String") && //
					!entry.getValue().getName().equals("Integer")) {
				if (entry.getValue() instanceof ModelClass) {
					GenInterfaceHelper.generateInterface((ModelClass) entry.getValue(), objects);
					GenClassHelper.generateClass((ModelClass) entry.getValue(), objects);
				}
			}
		}
		//
		for (Entry<String, IDatatype> entry : objects.entrySet()) {
			if (!entry.getValue().getName().equals("Boolean") && //
					!entry.getValue().getName().equals("String") && //
					!entry.getValue().getName().equals("Integer")) {
				//
				System.err.println("UML_"+entry.getValue().getName()+"=ar.com.coonocer.joy.mdg.xmi." + entry.getValue().getLabel() + ".impl."+entry.getValue().getName()+"Impl");
				if (entry.getValue() instanceof ModelClass) {
					GenInterfaceHelper.generateInterface((ModelClass) entry.getValue(), objects);
					GenClassHelper.generateClass((ModelClass) entry.getValue(), objects);
				}
			}
		}
	}

	static String upperInitial(String name) {
		return name.substring(0, 1).toUpperCase() + name.substring(1);
	}

	static String generateDataTypeForMany(ModelAttribute att, Map<String, IDatatype> objects) {
		String singularDatatype = getTypeName(att.type, objects);
		if (att.many) {
			return "List<" + singularDatatype + ">";
		} else {
			return singularDatatype;
		}
	}

	static String getTypeName(String typeId, Map<String, IDatatype> objects) {
		IDatatype type = objects.get(typeId);
		if (type == null) {
			throw new RuntimeException("Type does not exist. Id:" + typeId);
		}
		//
		return type.getName();
	}

	public static String getVariableName(String name) {
		if (name.equals("package")) {
			return "package_";
		}
		return name;
	}

}
